package com.qa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qa.common.ServerResponse;
import com.qa.dao.CommentMapper;
import com.qa.pojo.Article;
import com.qa.pojo.Comment;
import com.qa.pojo.User;
import com.qa.service.IArticleService;
import com.qa.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author
 * @Date 2020/3/8 23:37
 * @Desc
 **/
@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private IArticleService articleService;

    @Override
    public List<Comment> getByArticelId(Integer id) {
        return commentMapper.getByArticleId(id);
    }

    @Override
    public List<Comment> getByCommentId(Integer id) {
        return commentMapper.getByCommentId(id);
    }

    @Override
    public ServerResponse addComment(Integer commentId, String comment, User user,  Integer category) {
        Comment comment1 = commentMapper.selectByPrimaryKey(commentId);
        comment1.setCommentId(comment1.getId());
        comment1.setId(null);
        comment1.setFromEmail(user.getEmail());
        comment1.setFromHeadImg(user.getHeadImg());
        comment1.setFromId(user.getId());
        comment1.setFromName(user.getName());
        comment1.setConteng(comment);
        comment1.setCategory(category);

        int insert = commentMapper.insert(comment1);
        if (insert > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }

        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public ServerResponse addCommentCategory(String comment, User user, Integer articleId) {
        Article article = articleService.getById(articleId);
        Comment comment1 = new Comment();
        comment1.setCategory(1);
        comment1.setConteng(comment);
        comment1.setFromName(user.getName());
        comment1.setFromId(user.getId());
        comment1.setFromHeadImg(user.getHeadImg());
        comment1.setFromEmail(user.getEmail());
        comment1.setArticleId(articleId);
        comment1.setStatus(1);
        comment1.setToName(article.getAuthor());
        comment1.setToEmail(article.getEmail());
        int insert = commentMapper.insert(comment1);
        if (insert > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }

        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public PageInfo<Comment> getByComment(String comment, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> comments = commentMapper.getByComment(comment);
        return new PageInfo<>(comments);
    }

    @Override
    public Comment getById(Integer commentId) {
        return commentMapper.selectByPrimaryKey(commentId);
    }

    @Override
    public ServerResponse updateStatus(Comment comment) {
        int insert = commentMapper.updateStatus(comment.getId(), comment.getStatus());
        if (insert > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }

        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public int getCount(String beginTime, String endTime) {
        return commentMapper.getCount(beginTime, endTime);
    }
}
